package com.nandi.deviceinstall.ui.adapter

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.bean.TapRecordBean
import com.nandi.deviceinstall.common.Constant
import com.nandi.deviceinstall.util.SharedTool
import kotlinx.android.synthetic.main.item_record.view.*

class TapRecordAdapter : BaseQuickAdapter<TapRecordBean.DataBean.RecordsBean, BaseViewHolder> {
    var context: Context? = null
    constructor(layoutResId: Int) : super(layoutResId)

    override fun convert(helper: BaseViewHolder, item: TapRecordBean.DataBean.RecordsBean?) {
        if (context == null){
            context = helper.getView<ImageView>(R.id.ivRecord).context
        }

        helper.setText(R.id.disasterName, item!!.disasterName)
                .setText(R.id.disasterNum, item!!.disNo)
                .setText(R.id.cityName, item!!.cityName)
                .setText(R.id.townName, item!!.townName)
                .setText(R.id.time,item!!.addTime)
                .addOnClickListener(R.id.delete)
        if (item.pointPic!!.isNotEmpty()){
            var imagePath = "${Constant.fileUrl}${item.pointPic!!}&type=6"
            val headers = LazyHeaders.Builder().addHeader("Authorization", SharedTool.get(context,
                    Constant.TOKEN, "") as String).build()
            val requestOptions = RequestOptions().error(R.mipmap.default_image)
                    .placeholder(R.mipmap.default_image).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
            Glide.with(context!!)
                    //.asBitmap()
                    .load(GlideUrl(imagePath, headers))
                    .apply(requestOptions)
                    .into(helper.getView<ImageView>(R.id.ivRecord))
        }
    }
}